﻿#include"Game.h"

int tempCnt[8] = { 0 };
int dx[8] = { -1,-1,0,1,1,1,0,-1 };
int dy[8] = { 0,1,1,1,0,-1,-1,-1 };
int weight[ROL][COL] = { 0 };
void menu1()
{
	printf("********************\n");
	printf("******1.执子 *******\n");
	printf("******0.返回 *******\n");
	printf("********************\n");
}
void menu()
{
	printf("********************\n");
	printf("*******五子棋*******\n");
	printf("******1.开始游戏 ***\n");
	printf("******0.退出游戏 ***\n");
	printf("********************\n");
}
void InitGame()
{
	_wsetlocale(LC_ALL, L"chs");
	system("mode con  cols=150 lines=40");
	system("title 五子棋 游戏");
}
void GameStart(int chessBoard[ROL][COL])
{
	InitGame();//初始化
	int choice, choice2;
	int winner = 0;
	do
	{
		menu();
		printf("请选择：");
		scanf("%d", &choice);
		switch (choice)
		{
		case 1:
			system("cls");
			do
			{
				menu1();
				printf("请选择：");
				scanf("%d", &choice2);
				switch (choice2)
				{
				case 1:
					system("cls");
					UpDate(chessBoard);//更新界面
					PlayerTurn(chessBoard, White);//玩家回合
					ComputerTurn(chessBoard);//电脑回合
					system("cls");
					UpDate(chessBoard);//更新界面
					//检查胜利条件
					winner = CheckWin(chessBoard);
					if (winner == White)
					{
						printf("玩家获胜！！！\n");
						Freshen(chessBoard);//清空棋盘
					}
					else if(winner == Black)
					{
						printf("电脑获胜！！！\n");
						Freshen(chessBoard);//清空棋盘
					}
					break;
				case 0:
					system("cls");
					break;
				default:
					printf("输入有误，请重新输入\n");
					break;
				}
			} while (choice2);
			break;
		case 0:
			printf("退出游戏！");
			break;
		default:
			printf("输入有误，请重新输入!\n");
		}
	} while (choice);

}
int CheckPos(int x, int y)
{
	if (x < 0 || x > ROL - 1 || y < 0 || y > ROL - 1)
	{
		return 0;
	}
	return 1;
}
int CheckChess(int chessBoard[ROL][COL], int x, int y)
{
	if (chessBoard[x][y] == 0)
	{
		return 1;
	}
	return 0;
}
void SetChess(int chessBoard[ROL][COL], int flg)
{
	int x, y;
again:
	printf("请输入坐标：");
	scanf("%d%d", &x, &y);
	if (!CheckPos(x, y))
	{
		printf("输入坐标非法，请重新输入！\n");
		goto again;
	}
	else
	{
		if (CheckChess(chessBoard, x, y))
		{
			chessBoard[x][y] = flg;
		}
		else
		{
			printf("该位置不可执子！\n");
			goto again;

		}
	}
}
void PlayerTurn(int chessBoard[ROL][COL], int flg)
{
	SetChess(chessBoard, flg);//执棋
}

void UpDate(int chessBoard[ROL][COL])
{
	PrintBoard(chessBoard);
	//定位鼠标打印提示信息.......
}

void PrintBoard(int chessBoard[ROL][COL])
{
	printf("     ");
	for (int k = 1; k < ROL; k++)
	{
		if (k < 10)
		{
			printf("%d ", k);
		}
		else
		{
			printf("%d", k);
		}
	}
	printf("\n");
	for (int i = 0; i < ROL; i++)
	{
		if (0 == i)
		{
			printf("%3d",i);
			for (int j = 0; j < COL; j++)
			{
				if (0 == j)
				{
					Print(chessBoard[i][j], "┌ ");
				}
				else if (COL - 1 == j)
				{
					Print(chessBoard[i][j], "┐ ");
				}
				else
				{
					Print(chessBoard[i][j], "┬ ");
				}
			}
			printf("\n");
		}
		else if (14 == i)
		{
			printf("%3d", i);
			for (int j = 0; j < COL; j++)
			{
				if (0 == j)
				{
					Print(chessBoard[i][j], "└ ");
				}
				else if (COL - 1 == j)
				{
					Print(chessBoard[i][j], "┘");
				}
				else
				{
					Print(chessBoard[i][j], "┴ ");
				}
			}
			printf("\n");
		}
		else
		{
			printf("%3d", i);
			for (int j = 0; j < COL; j++)
			{
				if (0 == j)
				{
					Print(chessBoard[i][j], "├ ");
				}
				else if (COL - 1 == j)
				{
					Print(chessBoard[i][j], "┤ ");
				}
				else
				{
					Print(chessBoard[i][j], "┼ "); 
				}
			}
			printf("\n");
		}
	}
}
void Print(int type, char* wall)
{
	if (type == White)
	{
		wprintf(L"%lc", Playerchess);
	}
	if (type == Black)
	{
		wprintf(L"%lc", Computerchess);
	}
	if (type == 0)
	{
		printf("%s", wall);
	}
}
int* Count(int chessBoard[ROL][COL], int x, int y, int flg)
{
	memset(tempCnt, 0, sizeof(tempCnt));
	for (int i = 0; i < 8; i++)//八个方向
	{
		int px = x;
		int py = y;
		for (int j = 0; j < 4; j++)//每个方向试探四次
		{
			//先走一步
			px += dx[i];
			py += dy[i];
			if (CheckPos(px, py) && chessBoard[px][py] == flg)
			{
				tempCnt[i]++;
			}
			else
			{
				break;
			}
		}
	}
	return tempCnt;
}
int PlayerWin(int chessBoard[ROL][COL],int x,int y,int flg)
{
	int* count = Count(chessBoard, x, y, flg);
	for (int i = 0; i < 8; i++)
	{
		if (*(count + i) >= 4)
		{
			return White;
		}
	}
	return None;
}

int ComputerWin(int chessBoard[ROL][COL], int x, int y, int flg)
{
	int* count = Count(chessBoard, x, y, flg);
	for (int i = 0; i < 8; i++)
	{
		if (*(count + i) >= 4)
		{
			return Black;
		}
	}

	return None;
}
int CheckWin(int chessBoard[ROL][COL])
{
	int computer, player;
	for (int i = 0; i < ROL; i++)
	{
		for (int j = 0; j < COL; j++)
		{
			if (chessBoard[i][j] == 0)//没有棋子的情况下
				continue;
			else if (chessBoard[i][j] == White)//玩家棋子
			{
				player = PlayerWin(chessBoard, i, j, chessBoard[i][j]);
				if (player == White)
				{
					return White;
					//玩家获胜
				}
			}
			else//电脑棋子
			{
				computer = ComputerWin(chessBoard, i, j, chessBoard[i][j]);
				if (computer == Black)
				{
					return Black;
					//电脑获胜
				}
			}
		}
	}
	return None;
}

void Freshen(int chessBoard[ROL][COL])
{
	for (int i = 0; i < ROL; i++)
	{
		for (int j = 0; j < COL; j++)
		{
			chessBoard[i][j] = None;
		}
	}
}


void UpdateWeight(int chessBoard[ROL][COL])
{
	int aiWeight = 0, playerWeight = 0;
	for (int i = 0; i < ROL; i++)
	{
		for (int j = 0; j < COL; j++)
		{
			aiWeight = playerWeight = 0;
			if (!chessBoard[i][j] == None)
			{
				weight[i][j] = 0;
			}
			else
			{
				int* count = Count(chessBoard, i, j, White);
				for (int k = 0; k < 8; k++)
				{
					playerWeight += (int)pow(10, count[k]);
				}
			
				count = Count(chessBoard, i, j, Black);
				for (int k = 0; k < 8; k++)
				{
					aiWeight += (int)pow(10, count[k]);
				}
				
			}
			weight[i][j] = aiWeight > playerWeight ? aiWeight : playerWeight;
		}
	}
}

void ComputerTurn(int chessBoard[ROL][COL])
{
	UpdateWeight(chessBoard);
	int maxWeight = 0;
	int maxi, maxj;
	for (int i = 0; i < ROL; i++)
	{
		for (int j = 0; j < COL; j++)
		{
			if (weight[i][j] > maxWeight)
			{
				maxWeight = weight[i][j];
				maxi = i;
				maxj = j;
			}
		}
	}
	chessBoard[maxi][maxj] = Black;
}